<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        v-model="form"
        v-bind="bindVal"
        v-on="onEvent"
        :page.sync="page"
        :permission="permissionList"
      >
        <template slot="menuLeft">
	 <el-upload
                class="upload-inline"
                action=""
                :show-file-list="false"
                accept=".xls"
                :http-request="handleImport"
              >
                <el-button type="primary" size="small">导入</el-button>
              </el-upload>        
        </template>
	 <template slot="menuRight">
             <el-button
                @click="handleDownloadClickKY"
                type="text"
                size="small"
              >模板下载</el-button>
         </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import baseCrud from "@/mixins/base-crud";
import {tvmCodeImport,templateKY } from "@/api/support/swpmtvmcode";
import { handleExcelResponse } from "@/util/util";
import { mapGetters } from "vuex";
export default {
  name: "swpmtvmcode",
  mixins: [baseCrud("support/swpmtvmcode")],
  data() {
    return {};
  },
  created() {},
  computed: {
    bindVal() {
      const userLineCode = this.$store.getters.userInfo.lineCode;
      const userStationCode = this.$store.getters.userInfo.stationCode;
      const lineCode = this.tableOption.column.find(c => c.prop === "lineCode");
      const stationCode = this.tableOption.column.find(
        c => c.prop === "stationCode"
      );
      if (userStationCode !== "") {
        lineCode.value = userLineCode;
        lineCode.disabled = true;
        lineCode.search = false;
        lineCode.hide = true;

        stationCode.value = userStationCode;
        stationCode.disabled = true;
        stationCode.search = false;
        stationCode.hide = true;
      } else {
        lineCode.value = "";
        lineCode.disabled = false;
        lineCode.search = true;
        lineCode.hide = false;

        stationCode.value = "";
        stationCode.disabled = false;
        stationCode.search = true;
        stationCode.hide = false;
      }

      return {
        ref: "crud",
        option: this.tableOption,
        data: this.tableData,
        tableLoading: this.tableLoading,
        page: this.page
      };
    },
    permissionList() {
      return {
        addBtn: this.vaildData(
          this.permissions.support_sw_pm_tvm_code_add,
          false
        ),
        delBtn: this.vaildData(
          this.permissions.support_sw_pm_tvm_code_del,
          false
        ),
        editBtn: this.vaildData(
          this.permissions.support_sw_pm_tvm_code_edit,
          false
        )
      };
    }
  },
  methods: {
    handleImport(item) {
      this.$message.info("正在导入，请耐心等待！");
      let file = item.file;
      let param = new FormData();
      param.append("file", file);
      tvmCodeImport(param).then(() => {
        this.$notify({
          title: "成功",
          message: "导入成功",
          type: "success",
          duration: 2000
        });
        this.getList(this.page);
      });
    },
    async handleDownloadClickKY () {//导出模板
      this.uploading = true;
      try {
        const res = await templateKY();
        handleExcelResponse(res);
      } finally {
        this.uploading = false;
      }
    },
  }
};
</script>
